package com.zeng.sim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.sim.controller.domain.SimMsgRecord;
import com.zeng.sim.controller.domain.request.PageDomain;
import com.zeng.sim.mapper.model.SimMsgRecordEntity;
import com.zeng.sim.service.ServerMsgService;
import com.zeng.sim.service.SimMsgRecordService;
import com.zeng.sim.util.SimConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-25T11:55
 */
@Service
public class ServerMsgServiceImpl implements ServerMsgService {

    @Autowired
    private SimMsgRecordService simMsgRecordService;

    /**
     * 服务端报文列表查询
     * @param msgRecord
     * @param pageDomain
     * @return
     */
    @Override
    public Page<SimMsgRecord> data(SimMsgRecord msgRecord, PageDomain pageDomain) {
        Page page = Page.of(pageDomain.getPage(), pageDomain.getLimit());
        LambdaQueryWrapper<SimMsgRecordEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SimMsgRecordEntity::getBusiType, SimConstant.BUSI_TYPE_02);
        if (StringUtils.hasLength(msgRecord.getChannelNo())) {
            wrapper.eq(SimMsgRecordEntity::getChannelNo, msgRecord.getChannelNo());
        }
        if (StringUtils.hasLength(msgRecord.getMsgCode())) {
            wrapper.eq(SimMsgRecordEntity::getMsgCode, msgRecord.getMsgCode());
        }
        if (StringUtils.hasLength(msgRecord.getMsgName())) {
            wrapper.like(SimMsgRecordEntity::getMsgName, msgRecord.getMsgName());
        }
        simMsgRecordService.page(page, wrapper);

        List<SimMsgRecordEntity> recordList = page.getRecords();
        List<SimMsgRecord> list = new ArrayList<>(recordList.size());
        for (SimMsgRecordEntity msgRecordEntity : recordList) {
            SimMsgRecord simMsgRecord = new SimMsgRecord();
            BeanUtils.copyProperties(msgRecordEntity, simMsgRecord);
            simMsgRecord.setMsgContent(null);
            list.add(simMsgRecord);
        }
        page.setRecords(list);

        return page;
    }

}
